package com.xlhj.boot.config;

import com.xlhj.boot.job.CronJob;
import com.xlhj.boot.job.SimpleJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: liucaijing
 * @description: ScheduleConfig
 * @date: 2021/11/1 12:31
 */
@Configuration
public class ScheduleConfig {

    @Bean
    public JobDetail simpleJobDetail() {
        return JobBuilder.newJob(SimpleJob.class).withIdentity("simpleJobDetail").storeDurably().build();
    }

    @Bean
    public Trigger simpleTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(20).repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(simpleJobDetail())
                .withIdentity("simpleTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public JobDetail cronJobDetail() {
        return JobBuilder.newJob(CronJob.class).withIdentity("cronJobDetail").storeDurably().build();
    }

    @Bean
    public Trigger cronTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/30 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(cronJobDetail())
                .withIdentity("cronTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
